/*
 * @Descripttion:
 * @Author: Silence
 * @Date: 2023-06-03 19:40:22
 * @LastEditors: Silence
 * @LastEditTime: 2023-06-05 22:23:43
 */
import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '../store'

Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/login/index.vue')
    // /* webpackChunkName: "about" */  魔法注释
    /* 文件分类打包 */
  },
  {
    path: '/',
    name: 'layout',
    redirect: '/dashboard',
    component: () => import('../views/layout/index.vue'),
    children: [
      {
        path: '/dashboard',
        name: 'dashboard',
        component: () => import('../views/dashboard/index.vue')
      },
      {
        path: '/article',
        name: 'article',
        component: () => import('../views/article/index.vue')
      }
    ]
  }
]

const router = new VueRouter({
  routes
})
router.beforeEach((to, from, next) => {
  const { token } = store.state.user
  if (token) {
    next()
    return
  } else if (to.path == '/login') {
    next()
    return
  }
  next('/login')
})
export default router
